package dao.mysql;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import util.DataBaseConnection;

import bean.Distrito;
import dao.Entidad;

public  class MySQLDistritoDAO implements Entidad<Distrito>{

	
	@Override
	public Distrito insert(Distrito t) {
		/*Connection con=null;
		try {
			con=DataBaseConnection.openConnection();
			String sql="INSERT INTO Distrito(id_Distrito, id_Provincia, id_Departamento,";
			sql = sql + "id_Pais, nombre) VALUES(?,?,?,?,?)";
			PreparedStatement ps=con.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
			ps.setInt(1, t.getIdDistrito());
			ps.setInt(2, t.getIdProvincia());
			ps.setInt(3, t.getIdDepartamento());
			ps.setInt(4, t.getIdPais());
			ps.setString(5, t.getNombreDistrito());
			
			ps.executeUpdate();
			ResultSet rs=ps.getGeneratedKeys();
			if(rs.next()){
				int id=rs.getInt(1);
				t.setIdDistrito(id);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}*/
		return t;
	}

	@Override
	public int update(Distrito t) {
		
		/* Connection con=null;
		int rows = -1;
		try {
			con=DataBaseConnection.openConnection();
			String sql="UPDATE Distrito SET nombre=?";
			sql = sql +	"WHERE id_Distrito=? and id_Pais=? and id_Departamento=? and id_Provincia=?";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setInt(1, t.getIdDistrito());
			ps.setString(2, t.getNombreDistrito());
			ps.setInt(3, t.getIdPais());
			ps.setInt(4, t.getIdDepartamento());
			ps.setInt(3, t.getIdProvincia());
						
			rows =ps.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}*/
		return -1;
	}

	@Override
	public int delete(Distrito t) {
		/*Connection con=null;
		int rows=-1;
		try {
			con=DataBaseConnection.openConnection();
			String sql="DELETE FROM distrito";
			sql = sql +	"WHERE id_Distrito=? and id_Pais=? and id_Departamento=? and id_Provincia=?";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setInt(1, t.getIdDistrito());
			ps.setInt(2, t.getIdPais());
			ps.setInt(3, t.getIdDepartamento());
			ps.setInt(4, t.getIdProvincia());
			rows=ps.executeUpdate();
			System.out.println(rows);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}*/
		return -1;
	}

	@Override
	public Distrito findById(Distrito t) {
		/*Connection con=null;
		try {
			con=DataBaseConnection.openConnection();
			String sql="SELECT * FROM Distrito d, Provincia pr, Depatamento de, Pais pa" +
					" WHERE d.id_Distrito=? and d.id_Provincia = pr.id_Provincia" +
					" and d.id_Departamento = de.id_Departamento and d.id_Pais = pa.id_Pais";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setInt(1, t.getIdDistrito());
			ResultSet rs=ps.executeQuery();
			while(rs.next()){
				t.setIdDistrito(rs.getInt("id_Distrito"));
				t.setNombreDistrito(rs.getString("nombre"));
				t.setIdProvincia(rs.getInt("id_Provincia"));
				t.setNombreProvincia(rs.getString("nombre"));
				t.setIdDepartamento(rs.getInt("id_Departamento"));
				t.setNombreDepartamento(rs.getString("nombre"));
				t.setIdPais(rs.getInt("id_Pais"));
				t.setNombrePais(rs.getString("nombre"));		
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}*/
		return t;
	}

	@Override
	public List<Distrito> listAll() {
		/*List<Distrito> distrito = new ArrayList<Distrito>();
		Connection con=null;
		try {
			con=DataBaseConnection.openConnection();
			String sql="SELECT * FROM Distrito d, Provincia pr, Depatamento de, Pais pa" +
					" WHERE d.id_Provincia = pr.id_Provincia" +
					" and d.id_Departamento = de.id_Departamento and d.id_Pais = pa.id_Pais";;
			Statement st=con.createStatement();
			ResultSet rs=st.executeQuery(sql);
			while(rs.next()){
				Distrito t = new Distrito();
				t.setIdDistrito(rs.getInt("id_Distrito"));
				t.setNombreDistrito(rs.getString("nombre"));
				t.setIdProvincia(rs.getInt("id_Provincia"));
				t.setNombreProvincia(rs.getString("nombre"));
				t.setIdDepartamento(rs.getInt("id_Departamento"));
				t.setNombreDepartamento(rs.getString("nombre"));
				t.setIdPais(rs.getInt("id_Pais"));
				t.setNombrePais(rs.getString("nombre"));	
				distrito.add(t);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}*/
		return null;
	}

	@Override
	public List<Distrito> listAllCustom(Object... objects) {
		String id = (String)objects[0];
		List<Distrito> distrito = new ArrayList<Distrito>();
		Connection con=null;
		try {
			con=DataBaseConnection.openConnection();
			String sql="SELECT * FROM distrito d WHERE d.id_prov = "+id+" order by d.nomb_dist asc";
			Statement st=con.createStatement();
			ResultSet rs=st.executeQuery(sql);
			while(rs.next()){
				Distrito t = new Distrito();
				t.setId_dist(rs.getInt("id_dist"));
				t.setNomb_dist(rs.getString("nomb_dist"));
				t.setId_prov(rs.getInt("id_prov"));
				t.setId_depa(rs.getInt("id_depa"));
				t.setId_pais(rs.getInt("id_pais"));
				distrito.add(t);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return distrito;
	}

}
